#include "WOPInvokedObj.h"
#include "MappingEngine.h"
#include "all_entries.h"
#include "WOPEvent.h"


WOPInvokedObj::WOPInvokedObj(){

}
WOPInvokedObj::WOPInvokedObj(int lineNum,WOP_Entry* en){
	this->_lineNum = lineNum;
	this->_en = en;
	std::stringstream ss;	
	_type = en->getTypeStr();
	if(_type == "int"){
		WOP_Int_Proxy* wop_ip = dynamic_cast<WOP_Int_Proxy*>(en);
		this->_evt = new WOPEvent(wop_ip->getEntry()->getVal(),wop_ip->getEntry()->getVal(),_type);
	}
	else if(_type == "double"){
		WOP_Double_Proxy* wop_dp = dynamic_cast<WOP_Double_Proxy*>(en);
		this->_evt = new WOPEvent(wop_dp->getEntry()->getVal(),wop_dp->getEntry()->getVal(),_type);
	}
	else if(_type == "float"){
		WOP_Float_Proxy* wop_fp = dynamic_cast<WOP_Float_Proxy*>(en);
		this->_evt = new WOPEvent(wop_fp->getEntry()->getVal(),wop_fp->getEntry()->getVal(),_type);
	}
}

void WOPInvokedObj::updateProxyWOP(std::string wopvalue){
	if(_type == "int"){
		((WOP_Int_Proxy*)(this->_en))->updateProxyValue(wopvalue);
	}
	else if(_type == "double"){
		((WOP_Double_Proxy*)(this->_en))->updateProxyValue(wopvalue);
	}
	else if(_type == "float"){
		((WOP_Float_Proxy*)(this->_en))->updateProxyValue(wopvalue);
	}
}

void WOPInvokedObj::render(float timeSinceLastFrame){
	if(this->_evt != NULL){
		if(!this->_evt->checkInterpolationValueIsZero()){
			if(!this->_evt->checkBounding()){
				this->_evt->writePastValueToVariation();
			}		
			updateProxyWOP(this->_evt->getVariation());
		}
	}
}

void WOPInvokedObj::recover(){
	if(this->_evt->getCurrentValue() != this->_en->getVal()){
		updateProxyWOP(this->_evt->getCurrentValue());
	}
}


WOPInvokedObj::~WOPInvokedObj(){

}